import { login } from '@/api/user'
import { setItem } from '@/utils/storage/session.js'
export const user = {
  namespaced: true,
  state: {
    token: ''
  },
  mutations: {
    setToken (state, payload) {
      state.token = payload
      setItem('token', { data: payload })
    }
  },
  getters: {},
  actions: {
    login ({ commit }, payload) {
      return new Promise((resolve, reject) => {
        login(payload)
          .then((res) => {
            if (res.data.code === 200) {
              commit('setToken', res.data.data.token)
              resolve(res.data.code)
            } else {
              resolve(res.data.code)
            }
          })
          .catch((err) => {
            reject(err)
          })
      })
    }
  }
}
